{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2ac339f8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-31T05:37:48.611394Z",
     "start_time": "2025-01-31T05:37:41.800056Z"
    }
   },
   "source": [
    "import torch\n",
    "import matplotlib.pyplot as plt\n",
    "from torchvision import datasets, transforms,utils"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "ac1622c1",
   "metadata": {},
   "source": [
    "data_dir = './MNIST'"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "58e8ca13",
   "metadata": {},
   "source": [
    "dataset_train = datasets.MNIST(data_dir, download=False, train=True, transform=transforms.ToTensor())\n",
    "dataloader_test = torch.utils.data.DataLoader(dataset_train, 64)"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "628dd531",
   "metadata": {
    "scrolled": true
   },
   "source": [
    "data_iter = iter(dataloader_test)\n",
    "print(next(data_iter))"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "a4b15f39",
   "metadata": {},
   "source": [
    "oneimg,label = dataset_train[0]\n",
    "# oneimg = oneimg.numpy().transpose(1,2,0) \n",
    "# std = [0.5]\n",
    "# mean = [0.5]\n",
    "# oneimg = oneimg * std + mean\n",
    "oneimg = oneimg.reshape(28,28)\n",
    "print(label)\n",
    "plt.imshow(oneimg)\n",
    "plt.axis('off')\n",
    "plt.show()"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "b45b2974",
   "metadata": {},
   "source": [
    "oneimg,label = dataset_train[0]\n",
    "grid = grid.numpy().transpose(1,2,0) \n",
    "std = [0.5]\n",
    "mean = [0.5]\n",
    "grid = grid * std + mean\n",
    "plt.imshow(grid)\n",
    "plt.show()"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "ca353ac3",
   "metadata": {},
   "source": [
    "images, lables = next(iter(dataloader_test))\n",
    "img = utils.make_grid(images, padding=2)\n",
    "img = img.numpy().transpose(1,2,0)\n",
    "# std = [0.5]\n",
    "# mean = [0.5]\n",
    "# img = img * std + mean\n",
    "for i in range(64):\n",
    "    print(lables[i].item(), end=\" \")\n",
    "    i += 1\n",
    "    if i%8 == 0:\n",
    "        print(end='\\n')\n",
    "plt.subplots_adjust(left=0.2, right=0.8, top=0.9, bottom=0.1)\n",
    "plt.imshow(img)\n",
    "plt.axis('off')\n",
    "plt.show()"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "3e4e21d6",
   "metadata": {
    "scrolled": true
   },
   "source": [
    "# 演示make_grid的用法\n",
    "import torch\n",
    "import torchvision\n",
    "import matplotlib.pyplot as plt\n",
    "# 创建一些示例图片\n",
    "images = torch.randn(16, 3, 64, 64)  # 16 张 64x64 的彩色图片\n",
    "# 将图片制作成网格\n",
    "grid_img = torchvision.utils.make_grid(images, nrow=4, padding=2)\n",
    "# 可视化网格图片\n",
    "plt.figure(figsize=(10, 10))\n",
    "plt.imshow(grid_img.permute(1, 2, 0))  # 调整通道顺序以适应 matplotlib 的要求\n",
    "plt.axis('off')\n",
    "plt.show()"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "ac87854c",
   "metadata": {
    "scrolled": true
   },
   "source": [
    "import torch\n",
    "from torchvision import models, transforms\n",
    "from torchvision.models import ResNet101_Weights\n",
    "from PIL import Image\n",
    "\n",
    "resnet = models.resnet101(weights=ResNet101_Weights.IMAGENET1K_V1)\n",
    "resnet\n"
   ],
   "outputs": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
